import * as vscode from 'vscode';
import { DbcEditorProvider } from './dbcEditorProvider';

export function activate(context: vscode.ExtensionContext) {
	// 注册自定义编辑器
	const provider = new DbcEditorProvider(context);
	const disposable = vscode.window.registerCustomEditorProvider(
		DbcEditorProvider.viewType,
		provider,
		{
			webviewOptions: {
				retainContextWhenHidden: true,
			},
			supportsMultipleEditorsPerDocument: false,
		}
	);

	context.subscriptions.push(disposable);

	// 注册命令
	const openEditorCommand = vscode.commands.registerCommand('dbcEditor.openEditor', (uri: vscode.Uri) => {
		vscode.commands.executeCommand('vscode.openWith', uri, DbcEditorProvider.viewType);
	});

	// 注册解析文件快捷键命令
	const parseFileCommand = vscode.commands.registerCommand('dbcEditor.parseFile', () => {
		// 获取所有可见的编辑器
		const visibleEditors = vscode.window.visibleTextEditors;
		
		// 检查是否有活动的编辑器并且是DBC文件
		if (vscode.window.activeTextEditor && 
			vscode.window.activeTextEditor.document.uri.path.endsWith('.dbc')) {
			
			// 向provider发送消息请求解析
			provider.parseCurrentFile();
		} else {
			vscode.window.showErrorMessage('没有打开DBC编辑器');
		}
	});

	context.subscriptions.push(openEditorCommand, parseFileCommand);
}

export function deactivate() {} 